<?php
/*
 * @Author: lokei
 * @Date: 2022-10-29 16:04:13
 * @LastEditors: lokei
 * @LastEditTime: 2022-10-31 01:06:46
 * @Description: 
 */
namespace App\Http\Controllers\Pay\Card;

use App\Common\Tools\ResultTool;
use App\Common\Tools\Sn;
use App\Http\Controllers\Controller;
use App\Models\Pay\Card\CardRecordModel;
use Illuminate\Http\Request;

class RecordController extends Controller {

    public function get(Request $request) {
        $record = CardRecordModel::where('id', '=', $request->input('id'))->first();
        $res = ResultTool::success();
        $res['data'] = $record;
        return $res;
    }

    public function list() {
        $items = CardRecordModel::get();
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        $res['data']['total'] = count($items);
        return $res;
    }

    public function ofMe(Request $request) {
        $user = app('auth')->user();
        $record = CardRecordModel::where('uid', '=', $user->uid)->where('card_id', '=', $request->input('card_id'))->first();
        $res = ResultTool::success();
        $res['data'] = $record;
        return $res;
    }

    public function ofMy(Request $request) {
        $user = app('auth')->user();
        $items = CardRecordModel::where('uid', '=', $user->uid)->with('card')->get();
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        return $res;
    }

    public function register(Request $request) {
        $user = app('auth')->user();
        $is_exist = CardRecordModel::where('uid', '=', $user->uid)->where('card_id', '=', $request->input('card_id'))->first();
        if(!is_null($is_exist)) {
            $res = ResultTool::fail();
            if($is_exist->status == '0') {
                $res['errorMsg'] = '信息已提交';
            } else {
                $res['errorMsg'] = '已开通';
            }
            return $res;
        }
        $record = new CardRecordModel();
        $record->sn = Sn::build_order_no();
        $record->uid = $user->uid;
        $record->card_id = $request->input('card_id');
        $record->name = $request->input('name');
        $record->mobile = $request->input('mobile');
        $record->company = $request->input('company');
        $record->id_number = $request->input('id_number');
        $record->img = $request->input('img');
        $record->money = 0;
        $record->status = '0';
        $record->created_at = date('Y-m-d H:i:s', time());
        $record->save();
        $res = ResultTool::success();
        $res['data'] = $record;
        return $res;
    }

    public function delete(Request $request) {
        CardRecordModel::where('id', '=', $request->input('id'))->delete();
        return ResultTool::success();
    }
    
    public function verify(Request $request) {
        $record = CardRecordModel::where('id', '=', $request->input('id'))->first();
        $record->status = '1';
        $record->save();
        return ResultTool::success();
    }
}